package org.acompanhamento.domain;

import static javax.persistence.FetchType.LAZY;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import jmine.tec.aom.domain.Property;
import jmine.tec.persist.annotation.Alias;
import jmine.tec.persist.annotation.Index;

import org.acompanhamento.dao.TaskDAO;
import org.hibernate.annotations.Type;
import org.hibernate.validator.NotNull;

import bancosys.tec.component.Documentation;
import bancosys.tec.utils.date.Date;

/**
 * 
 * @author lundberg
 * 
 */
@Entity
@Table(name = "SPRINT")
@Alias("SPRINT")
@Documentation("AGILE TEAM ITERATIONS")
@SequenceGenerator(name = "SEQ_SPRINT", sequenceName = "SEQ_SPRINT")
public class Sprint extends PropertizedEntity {

	private Long id;

	private Date start;

	private Date end;

	private Team team;

	private String summary;

	private Workflow workflow;

	private Collection<Task> tasks;

	@Id
	@GeneratedValue(generator = "SEQ_SPRINT")
	@Column(name = "COD_SPRING", nullable = false)
	@Documentation("SPRINT ID")
	public Long getId() {
		return this.id;
	}

	@Type(type = "bancosys.tec.persist.hibernate.type.DateType")
	@Column(name = "DT_START", nullable = false)
	@Documentation("SPRINT START DATE")
	public Date getStart() {
		return this.start;
	}

	@Type(type = "bancosys.tec.persist.hibernate.type.DateType")
	@Column(name = "DT_END", nullable = false)
	@Documentation("SPRINT END DATE")
	public Date getEnd() {
		return this.end;
	}

	@Index(suffix = "0")
	@ManyToOne(fetch = LAZY)
	@JoinColumn(name = "COD_TEAM", nullable = false)
	@Documentation("TEAM")
	public Team getTeam() {
		return this.team;
	}

	@Column(name = "SUMMARY")
	@Documentation("SPRINT SUMMARY")
	public String getSummary() {
		return this.summary;
	}

	@ManyToOne(fetch = LAZY)
	@NotNull
	@JoinColumn(name = "COD_WORKFLOW")
	@Documentation("WORKFLOW ID")
	public Workflow getWorkflow() {
		return this.workflow;
	}

	@OneToMany
	@Index(suffix="1")
	@JoinColumn(name = "COD_SPRING")
	@Documentation("SPRINT ID")
	public Collection<Task> getTasks() {
		return this.tasks;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public void setEnd(Date end) {
		this.end = end;
	}

	public void setTeam(Team team) {
		this.team = team;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public void setWorkflow(Workflow workflow) {
		this.workflow = workflow;
	}

	public void setTasks(Collection<Task> tasks) {
		this.tasks = tasks;
	}

	@Transient
	public List<Task> getTasksByStatus(TaskDAO taskDAO, Status status){
		return taskDAO.findBySprintStatus(this, status);
	}

	@Override
	public String toString() {
		return String.format("%s %s - %s", this.getTeam().getName(), this.getStart().getMessage(), this.getEnd().getMessage());
	}

	@Override
	@Transient
	protected Set<Property> getEntityProperties() {
		return Collections.emptySet();
	}
}
